package utils
{
	public class XmlUtil
	{
		private static var index:int=0;
		private static var targets:Array;
		/**
		 * 给指定的对象添加属性，假设对象包括target1,target2,target3,如果对象target1设置失败，那么就在对象target2上面设置属性,如果target2上面也设置失败，那么就在target3上面设置属性
		 */ 
		public static function setPropertys(targets:Array,xml:XML):void
		{
			if(xml==null) return;
			XmlUtil.targets=targets;
			if(targets!=null && targets.length>0)
			{
				for each(var item:XML in xml.attributes())
				{
					var key:String=item.name();
					var val:String=item.toString();
					if(index!=targets.length) setProperty(targets[index],key,val);
				}
				XmlUtil.targets=null;
				index=0;
			}else
			{
				targets=null;
			}
		}
		//设置某个对象的属性
		private static function setProperty(target:*,key:String,val:String):void
		{
			try
			{
				target[key]=val;
			}catch(e:Error)
			{
				if(!isEnd()) setProperty(targets[index],key,val);
			}
		}
		//属性是否全部设置完毕
		private static function isEnd():Boolean
		{
			index++;
			if(index==targets.length)
			{
				index=0;
				return true;
			}
			return false;
		}
	}
}